<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\shop\controller;

use app\admin\model\GoodsModel;
use app\store\model\StoreModel;
use cmf\controller\HomeBaseController;
use think\Db;

class IndexController extends HomeBaseController
{
    //前台首页
    public function index()
    {
        $param=$this->request->param();
        if(isset($param['id'])){
            cookie('store_id',$param['id']);
            $id=$param['id'];
        }else{
            if(cookie('store_id')){
                $id=cookie('store_id');
            }else{
                $id=null;
            }
        }
        $yylm_store_id=session('yylm_store_id');
        $uid=cmf_get_current_user_id();
        $flag=0;
        if($yylm_store_id&&$uid){
            $where['role_id']=4;
            $where['user_id']=$uid;
            $yylm_rs=Db::name('role_user')->where($where)->find();
            if($yylm_rs){
                if($yylm_rs['store_id']==0){
                    $data['store_id']=$yylm_store_id;
                    Db::name('role_user')->where('id',$yylm_rs['id'])->update($data);
                     $flag=1;
                }else{
                    $flag=2;
                }
            }else{
                $where['store_id']=$yylm_store_id;
                Db::name('role_user')->insert($where);
                $flag=3;
            }
            session('yylm_store_id',null);
        }
        $store=new StoreModel();
        //店铺信息
        $store_info=$store->get_store_info($id,1);
        //会员卡信息
        $card_info=$store->get_card($store_info['id']);
        //积分商品
        $goodsmodel = new GoodsModel();
        //获取数量
        $type=0;
        $data        = $goodsmodel->adminGoodsList($param,$type);

        $page=ceil($data/9);
        $id=$store_info['id'];
        if(cmf_is_user_login()){
            $uid=cmf_get_current_user_id();
            $user_info=['store_id'=>$id,'id'=>$uid];
            Db::name("user")->update($user_info);
        }
        $this->assign('store_id',$id);
        $this->assign('flag',$flag);
        $this->assign('store_info',$store_info);
        $this->assign('card_info',$card_info);
        $this->assign('page',$page);
        return $this->fetch(':index');
    }
    //店铺导航
    public function baidu_search(){
        $param=$this->request->param();
        if(isset($param['id'])){
            cookie('store_id',$param['id']);
            $id=$param['id'];
        }else{
            if(cookie('store_id')){
                $id=cookie('store_id');
            }else{
                $id=null;
            }
        }
        $store=new StoreModel();
        //店铺信息
        $store_info=$store->get_store_info($id);
        $this->assign('store_id',$id);
        $this->assign('param',$param);
        $this->assign('store_info',$store_info);
        return $this->fetch(':baidu_search');
    }
    //店铺相册
    public function photo(){
        $param=$this->request->param();
        if(isset($param['id'])){
            cookie('store_id',$param['id']);
            $id=$param['id'];
        }else{
            if(cookie('store_id')){
                $id=cookie('store_id');
            }else{
                $id=null;
            }
        }
        $store=new StoreModel();
        //店铺信息
        $store_info=$store->get_store_info($id);
        $this->assign('store_id',$id);
        $this->assign('store_info',$store_info);
        return $this->fetch(':photo');
    }
    /**
     * ajax获取店铺列表
     */

    function get_store_list(){
        $store=new StoreModel();
        $param=$this->request->param();
        $baidu['lng']= $param['lng'];
        $baidu['lat']= $param['lat'];
        $store_list=$store->store_list($baidu,false)->toArray();
        foreach ($store_list['data'] as $key=>$val){
            $href=cmf_url('index',array('id'=>$val['id']));
            $store_list['data'][$key]['href']=$href;
        }
        return $store_list;
    }

    function weixin(){
        $param=$this->request->param();
        $defaultWechat=hook_one("weixinlogin",$param);
        dump($defaultWechat);
    }

    function get_shop_list(){
        $type=1;
        $param=$this->request->param();
        $goodsmodel = new GoodsModel();
        $data        = $goodsmodel->adminGoodsList($param,$type);
        $shop_list=$data->items();
        $new_shop=[];
        foreach($shop_list as $k=>$v){
            $url=cmf_url('shop/good/index',array('id'=>$v['id']));
            $new_shop[$k]['url']=$url;
            $new_shop[$k]['post_title']=$v['post_title'];
            $new_shop[$k]['id']=$v['id'];
            $new_shop[$k]['thumbnail']=cmf_get_image_preview_url($v['more']['thumbnail']);
            $new_shop[$k]['post_price']=$v['post_price'];
            $new_shop[$k]['post_score']=$v['post_score'];
        }
        $this->success('商品列表',null,$new_shop);
    }

}
